package com.fjh.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class SensitiveWordFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 在过滤器初始化时执行的操作，可以用于读取配置等
    }


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("SensitiveWordFilter is being called...");

        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest) request;

            if (httpRequest.getMethod().equalsIgnoreCase("POST")) {
                // 获取请求参数，进行敏感词过滤
                Map<String, String[]> parameterMap = request.getParameterMap();
                for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                    String paramName = entry.getKey();
                    String[] paramValues = entry.getValue();

                    if (paramValues != null && paramValues.length > 0) {
                        for (int i = 0; i < paramValues.length; i++) {
                            paramValues[i] = filterSensitiveWords(paramValues[i]);
                        }
                    }
                }

                // 使用包装类修改请求参数
                HttpServletRequestWrapper wrappedRequest = new HttpServletRequestWrapper(httpRequest) {
                    @Override
                    public String getParameter(String name) {
                        String[] values = getParameterValues(name);
                        if (values != null && values.length > 0) {
                            return values[0];
                        }
                        return null;
                    }

                    @Override
                    public Map<String, String[]> getParameterMap() {
                        return parameterMap;
                    }

                    @Override
                    public String[] getParameterValues(String name) {
                        return parameterMap.get(name);
                    }
                };

                chain.doFilter(wrappedRequest, response);
                return;
            }
        }

        chain.doFilter(request, response);
    }




    @Override
    public void destroy() {
        // 在过滤器销毁时执行的操作
    }

    private String filterSensitiveWords(String content) {

        // 实现敏感词过滤逻辑，使用正则表达式替换所有敏感词
        // 这里简单示例，假设敏感词列表存储在一个数组中
        String[] sensitiveWords = {"傻逼", "2B", };

        for (String sensitiveWord : sensitiveWords) {
            content = content.replaceAll("(?i)" + Pattern.quote(sensitiveWord), "*");
        }

        System.out.println("Filtered content: " + content);

        return content;
    }

}
